<?

/*

	The Logger class is used to maintain a persistant log file as
	efficiently as possible.  Using Logger, your programs can append log
	entries to as many different files as you need, using only 1 fopen()
	call and 1 fclose() call.

Use:
	
   include("Logger.class.php");
   $logger = new Logger("/path/to/log/file/root_directory");

    $logger->initialize(
                     array(
                            ERRLOG => 'error_log',
                            DEBUGLOG => 'debug_log'
                          )
                       );

   $logger->log(ERRLOG,"This is an error_log entry");
   $logger->log(DEBUGLOG,"This is logged to the debug_log");


    $logger->close();
    exit;



*/

define("INFOLOG", "info_log");
define("ERRLOG", "error_log");
define("DEBUGLOG", "debug_log");
define("WARNLOG", "warn_log");
define("FATALLOG", "fatal_log");

class Logger {

	var $FILELIST	=	array();
	var $PID		=	"";
	var $format		=	"%b %d %H:%M:%S";
	var $DEBUG		=	true;
	var $handles	= 	array();
	var $baseDir	= "";

	function __construct($LogRoot, $FileArray = "")
	{
		$this->PID = substr(uniqid("id"),-4,4);
		if(empty($LogRoot))
		{
			if($DEBUG)
			{
				error_log("[ErrorLog]: need LogRoot to initialize",0);
			}
			return;
		}
		$this->baseDir = $LogRoot . (substr($LogRoot,-1,1)!="/"? "/" : ""); 

		if(empty($FileArray)) {
			$FileArray = array(
                ERRLOG => 'error.log',
                FATALLOG => 'error.log',
                DEBUGLOG => 'debug.log',
                INFOLOG => 'info.log',
                WARNLOG => 'warn.log'
            );
		}
		$this->initialize($FileArray);
		return;
	}
	
	function setFormat($format) {
		$this->format =  $format;
	}

	function initialize($FileArray)
	{
		if ( (gettype($FileArray)) != "array")
		{
			if($DEBUG)
			{
				error_log("[ErrorLog]: invalid data type",0);
			}
			return;
		}

		while ( list ($key,$val) = each ($FileArray) )
		{
			if ( (!empty($key)) and (!empty($val)) )
			{
				$this->FILELIST[$key] = $this->baseDir . $val;
			}
		}
		return;
	}

	function log($Handle,$LogEntry) {
		global $php_errormsg;

		$filename = $this->FILELIST[$Handle];
		$TimeStamp = strftime($this->format,time());
		$LogEntry = "$TimeStamp [$this->PID] $LogEntry";
		if (!isset($this->handles[$filename]))
		{
			$fd = fopen($filename,"a");
			if ( (!$fd) or (empty($fd)) )
			{
				if($DEBUG)
				{
					error_log("[ErrorLog]: Fatal file error $php_errormsg",0);
				}
				return;
			}
			// fwrite($fd,"$TimeStamp [$this->PID] [ErrorLog]: Log File Opened\n");

			fwrite($fd,"$LogEntry\n");

			$this->handles[$filename] = $fd;
			// fclose($fd);
		}
		else
		{
			fwrite($this->handles[$filename],"$LogEntry\n");
		}
		return;
	}
	
	function debug($message) {
		$this->log(DEBUGLOG,$message);
	}
	
	function info($message) {
		$this->log(INFOLOG, $message);
	}
	
	function warn($message) {
		$this->log(WARNLOG,$message);
	}
	
	function error($message) {
		$this->log(ERRLOG,$message);
	}
	
	function fatal($message) {
		$this->log(FATALLOG,$message);
	}

	function close() {
		while ( list ($Handle, $Val) = each ($this->FILELIST) )
		{
			if(isset($this->handles[$Val]))
			{
				$TimeStamp = strftime($this->format,time());
				// fwrite($this->$Handle,"$TimeStamp [$this->PID] [ErrorLog]: Closing Log File\n");
				if (isset($this->handles[$Val])) {
					fclose($this->handles[$Val]);
					unset($this->handles[$Val]);
				}
			}
		}
	}
	
}	// End class.Logger

?>
